<?php

namespace App\Console\Commands\OpenShop;

use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Enums\OpenShop\InvestmentPromotion\SiteMissionEnum;
use App\Enums\OpenShop\MissionTemplateEnum;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSiteMissionModel;
use Illuminate\Console\Command;

class MissionDealTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'openShop:mission-deal-time';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '订单各任务处理时间导出';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $stepStatusLists = [
            MissionTemplateEnum::STEP_STATUS_3  => '开发待指派',
            MissionTemplateEnum::STEP_STATUS_4  => '选址待提交',
            MissionTemplateEnum::STEP_STATUS_5  => '商家待确认',
            MissionTemplateEnum::STEP_STATUS_6  => '建店信息待确认',
            MissionTemplateEnum::STEP_STATUS_7  => '设计师待指派',
            MissionTemplateEnum::STEP_STATUS_13 => '设计模块待确认',
            MissionTemplateEnum::STEP_STATUS_14 => '设计模块待支付',
            MissionTemplateEnum::STEP_STATUS_15 => '设备模块待确认',
            MissionTemplateEnum::STEP_STATUS_16 => '设备模块待支付',
            MissionTemplateEnum::STEP_STATUS_8  => '整套图待提交',
            MissionTemplateEnum::STEP_STATUS_17 => '软装模块待支付',
            MissionTemplateEnum::STEP_STATUS_9  => '进场待确认',
            MissionTemplateEnum::STEP_STATUS_10 => '施工待完成',
            MissionTemplateEnum::STEP_STATUS_11 => '督导待指派',
            MissionTemplateEnum::STEP_STATUS_12 => '督导待验收',
            MissionTemplateEnum::STEP_STATUS_18 => '开业报告待提交',
        ];

        $fds = OpenShopSignOrderModel::query()
                                     ->where('status', '>', SignOrderEnum::ORDER_STATUS_NO)
                                     ->get()
                                     ->map(function ($item) use ($stepStatusLists) {

                                         $missionLists = OpenShopSiteMissionModel::query()
                                                                                 ->where('orderNo', $item->orderNo)
                                                                                 ->where('status',
                                                                                     SiteMissionEnum::STATUS_2)
                                                                                 ->get()
                                                                                 ->keyBy('stepStatus');

                                         if (!isset($missionLists[MissionTemplateEnum::STEP_STATUS_3])) {
                                             return null;
                                         }

                                         $ffd = [$item->orderNo];

                                         foreach ($stepStatusLists as $stepStatus => $v) {
                                             if (!isset($missionLists[$stepStatus])) {
                                                 $ffd[] = '';
                                                 continue;
                                             }

                                             if ($missionLists[$stepStatus]->completeAt == 0) {
                                                 $ffd[] = '';
                                                 continue;
                                             }

                                             $ffd[] = $missionLists[$stepStatus]->completeAt - $missionLists[$stepStatus]->createdAt->timestamp;
                                         }

                                         return $ffd;
                                     })->filter();


        ini_set('max_execution_time', 100000);
        header("Content-Disposition: attachment; filename=" . urlencode('开店订单各任务耗时') . 'csv');

        // UTF8的BOM头
        $fd = fopen(public_path('/mission_time.csv'), 'w+');
        fwrite($fd, "\xEF\xBB\xBF");


        // 表头
        fputcsv($fd, array_merge([
            'orderNo',
        ], $stepStatusLists));


        $fds->each(function ($item) use ($fd) {
            fputcsv($fd, $item);
        });

        if (is_resource($fd)) {
            fclose($fd);
        }
        die;
    }
}
